/******************************************************************************
 *
 * Copyright:
 *    (C) 2005 Embedded Artists AB
 *
 * File:
 *    main.c
 *
 * Description:
 *    Sample application that demonstrates how to use the SPI interface
 *    with interrupts.
 *
 * Modified by:
 *      RTJ Computing Pty Ltd
 *****************************************************************************/
#include <lpc2xxx.h>
#include "general.h"
#include "spi.h"

spi_command_t spiCmd;

/*****************************************************************************
 *
 * Description:
 *    Actual spi ISR.
 *
 ****************************************************************************/
void spiISR(void) __attribute__ ((interrupt));
void spiISR(void) 
{
    volatile tU8 status;

    status = SPI_SPSR;

    // check current command
    if (spiCmd.cmd == SPI_COMMAND_WRITE) {
        // check if end of transmission
        if (spiCmd.len <= 1) {
            // deactivate SPI slave
            IOSET0 = spiCmd.cs;              
            // signal that transmission is ready
            spiCmd.ready = TRUE;      
        }
        else {
            spiCmd.buf++;
            SPI_SPDR = *spiCmd.buf;
            spiCmd.len--;
        }
    }
    else if (spiCmd.cmd == SPI_COMMAND_READ) {
        // get received data byte
        *spiCmd.buf = SPI_SPDR;   
        spiCmd.buf++;
        spiCmd.len--;
        // check if end of transmission
        if (spiCmd.len == 0) {
            // deactivate SPI slave
            IOSET0 = spiCmd.cs;              
            // signal that transmission is ready
            spiCmd.ready = TRUE;      
        }
        else 
            SPI_SPDR = 0xff;   // send dummy byte to ready next byte
    }
    SPI_SPINT   = 0x01;        // reset IRQ flag in spi
    VICVectAddr = 0x00;        // dummy write to VIC to signal end of interrupt
}

